package org.yaac.client.util;

import static com.google.common.base.Strings.isNullOrEmpty;

import javax.inject.Inject;

import org.yaac.client.i18n.ErrorMessages;
import org.yaac.shared.YaacException;


import com.google.gwt.user.client.Window;

/**
 * @author Max Zhu (thebbsky@gmail.com)
 *
 */
public class ExceptionManager {

	private final ErrorMessages errorMsg;
	
	@Inject
	ExceptionManager(ErrorMessages errorMsg) {
		super();
		this.errorMsg = errorMsg;
	}

	/**
	 * @param caught
	 */
	public void handle(Throwable caught) {
		if (caught instanceof YaacException) {
			YaacException e = (YaacException) caught;
			
			if (isNullOrEmpty(e.getErrorCode())) {
				Window.alert(e.getErrorMsg());
			} else {
				String msg = errorMsg.getString(e.getErrorCode());
				Window.alert(msg);
			}
		} else {	// unexpected exception
			caught.printStackTrace();
			Window.alert("Unexpected exception : " + caught.getMessage());
		}
	}
}
